package org.MobileSales;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.MobileSales.DataManagement.ClientsManager;
import org.MobileSales.DataManagement.TasksManager;
import org.MobileSales.Geolocalization.Geolocalization;
import org.MobileSales.Model.Clients.Client;
import org.MobileSales.Model.Tasks.Task;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;  
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
 
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
 
public class MapsActivity extends MapActivity 
{    
    MapView mapView; 
    GeoPoint p;
    MapController mc;
    private Date selectedDate;

 
    /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true); 
        
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
        MapOverlays itemizedoverlay = new MapOverlays(drawable);
        
        TasksManager tm = new TasksManager(this);
        selectedDate = new Date();
        List<Task> tasks = tm.GetOverlayTasks(selectedDate);//* 1E6
        ClientsManager cm = new ClientsManager(this);
        for (Task t : tasks){
        	if (t.ClientId != null && t.ClientId != 0){
        		Client c = cm.GetClient(t.ClientId);
        		StringBuilder sb = new StringBuilder();
        		sb.append(c.City);
        		sb.append(c.Street);
        		Location l = Geolocalization.reverseGeocode(this, sb.toString());
        		if (l!=null){
		        	GeoPoint gp = new GeoPoint((int) (l.getLatitude()*1E6), (int) (l.getLongitude()*1E6));

		        	OverlayItem overtask = new OverlayItem(gp,c.Name + " " + c.Name2,c.Street);
		        	itemizedoverlay.addOverlay(overtask);
        		}
        	}
        }
        mapOverlays.add(itemizedoverlay);
    }
 
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}
